#include <rasterizer.h>
#include <utility_math.h>
#include <vec4.h>

void Resterizer::rasterization(Vertex& vert1, Vertex& vert2, Vertex& vert3) {
    Bound2i box = AABBbox(vert1.position, vert2.position, vert3.position);
    for (auto point : box) {
        Vec2f target{ point.x + 0.5f, point.y + 0.5f };
        if (isInTriangle(vert1.position, vert2.position, vert3.position, target)) {
            auto [alpha, beta, gamma] = 
                barycentric(vert1.position, vert2.position, vert3.position, target);
            float targetZ = alpha * vert1.position.z + beta * vert2.position.z + gamma * vert3.position.z;
            if (targetZ < mBuffer.getPixelCurrentZ(point)) {
                mBuffer.getPixelCurrentZ(point) = targetZ;
                mBuffer.getPixelCurrentColor(point) = mShader->FragmentShader(alpha, beta, gamma);
            }
        }
    }
}